<template>
	<view>
		<view class="header">
			<view class="u-flex-nowrap u-row-between u-flex page-font">
				<view class="u-flex">请输入工单编号或扫码</view>
			</view>
			<view class="u-flex-nowrap u-row-around u-flex page-font">
				<u-form class="u-flex u-flex-nowrap u-row-around">
					<u-form-item>
						<u-input v-model="scanResult" :type="type" height=50 placeholder="请输入工单编号" clearable />
					</u-form-item>
					<view class="inputTips" v-if="user.departmentName == '喷涂车间'">提示：工单编号例如:5104-2306240002</view>
					<view class="inputTips" v-else>提示：工单编号例如:5103-2207010105</view>
					<!-- <u-form-item v-if="user.departmentName == '喷涂车间'">
						<u-input v-model="machineId" height=50 placeholder="请输入工位号" clearable />
					</u-form-item> -->
					<!-- <u-form-item v-if="user.departmentName == '喷涂车间'">
						<u-input v-model="completionQuantity" @blur="unsameData" type="number" height=50 placeholder="请输入数量" clearable />
					</u-form-item> -->
					<u-form-item class="u-flex u-row-center">
						<u-button size="medium" type="success" @click="getPtBindingUser">查询</u-button>
					</u-form-item>
				</u-form>
				<view class="u-flex-nowrap u-flex-col u-row-center u-col-center" @click="scanFun">
					<u-icon name="scan" color="#2979ff" size="100"></u-icon>
					<view class="$u-type-primary">点击扫码</view>
				</view>
			</view>
		</view>
		
		<!-- <view class="u-flex u-row-center u-col-center buttonGap" style="padding: 20rpx 0" v-if="user.departmentName == '喷涂车间'">
			<u-button size="medium" type="success" @click="isQualified">全部检查合格</u-button>
		</view> -->
		
		<!-- 条形码包含的工位信息 -->
		<view>
			<view class="list-item-wrapper" :style="onlineStyle[index]" v-bind:id="index" v-for="(item,index) in List" :key="index">
				<view style="padding: 20rpx">
					<view class="list-item" v-if="!item.otherprocess">
						<view>订单编号</view>
						<view>{{item.orderPlanNo}}</view>
					</view>
					<view class="list-item" v-if="!item.otherprocess">
						<view>元件编号</view>
						<view>{{item.component}}</view>
					</view>
					<view class="list-item" v-if="!item.otherprocess">
						<view>条码</view>
						<view>{{item.deviceBarcode}}</view>
					</view>
					<view class="list-item" v-if="item.otherprocess">
						<view>工序名称</view>
						<view>其他工序</view>
					</view>
					<view class="list-item" v-if="!item.otherprocess">
						<view>工艺名称</view>
						<view>{{item.processName}}</view>
					</view>
					<view class="list-item" v-if="!item.otherprocess && user.departmentName != '喷涂车间'">
						<view>扫码数量</view>
						<view>{{item.scanCount}}</view>
					</view>
					<!-- <view class="list-item" v-if="!item.otherprocess && user.departmentName == '喷涂车间'">
						<view>粉末类型</view>
						<u-input input-align="right" v-model="item.powderType" type="select" :select-open="showpowderType" @click="showpowderType = true;powIndex = index"/>
					</view>
					<view class="list-item" v-if="!item.otherprocess && user.departmentName == '喷涂车间'">
						<view>膜厚1</view>
						<u-input input-align="right" v-model="item.mohou1" type="digit"/>
					</view>
					<view class="list-item" v-if="!item.otherprocess && user.departmentName == '喷涂车间'">
						<view>膜厚2</view>
						<u-input input-align="right" v-model="item.mohou2" type="digit"/>
					</view>
					<view class="list-item" v-if="!item.otherprocess && user.departmentName == '喷涂车间'">
						<view>膜厚3</view>
						<u-input input-align="right" v-model="item.mohou3" type="digit"/>
					</view>
					<view class="list-item" v-if="!item.otherprocess && user.departmentName == '喷涂车间'">
						<view>膜厚4</view>
						<u-input input-align="right" v-model="item.mohou4" type="digit"/>
					</view>
					<view class="list-item" v-if="!item.otherprocess && user.departmentName == '喷涂车间'">
						<view>膜厚5</view>
						<u-input input-align="right" v-model="item.mohou5" type="digit"/>
					</view> -->
					<view class="operateBtn">
						<!-- show1 = true -->
						<!-- <u-button size="default" type="success" v-if="!item.otherprocess && user.departmentName == '喷涂车间'" @click="upSubmit(item,index)">检查合格</u-button> -->
						<u-button size="medium" type="success" v-if="!item.otherprocess && user.departmentName != '喷涂车间'" @click="isQualified(index)">检查合格</u-button>
						<u-button :size="user.departmentName == '喷涂车间'?'default':'medium'" type="warning" @click="goFillForm(item,index)">不合格</u-button>
						<!-- <u-button size="default" type="success" v-if="!item.otherprocess && user.departmentName == '喷涂车间'" @click="addRecord(item)">新增</u-button> -->
					</view>
				</view>
			</view>
		</view>
		<u-top-tips ref="uTips"></u-top-tips>
		<u-modal v-model="show" :content="content" :async-close="true" :show-cancel-button="cancleBtn" @cancel="cancel" @confirm="confirm"></u-modal>
		<u-modal v-model="show1" :content="content1"></u-modal>
		<u-modal v-model="showQualified" :content="content2" :show-cancel-button="cancleBtn" @cancel="cancelVerifiedModal" @confirm="confirmQualified"></u-modal>
		<u-action-sheet :list="powderTypeList" v-model="showpowderType" @click="powderTypeSelect"></u-action-sheet>
	</view>
</template>

<script>
	import { getQCView,getPtQCView,getPtBindingUser,machineStationScan,commitPtDisqualificationInWarehouse,saveMouHou } from '../../../utils/index.js'
	export default {
		data() {
			return {
				scanResult: '',							//条形码信息
				planNo: '',								//发送条形码后获取的planNo
				sectionNo: '',							//工段
				show: false,							//弹出框是否显示的条件
				content: '是否确认所有工序均已合格',		//弹出框的提示信息
				cancleBtn: true,						//弹出框的“取消”按钮显示条件
				List: [],								//条形码包含的制作工位信息
				onlineStyle: ["",""],	//当喷涂车间提交参数后填充的样式
				jointSwitch: true,
				show1: false,							//检查合格弹窗
				content1: '系统默认sop上一个工序扫码完成到下一个工序扫码完成，表示检查合格',
				user: uni.getStorageSync('user'),
				machineId: '',
				completionQuantity: '',
				showQualified: false,
				content2: '是否确认工序已合格',		//弹出框的提示信息
				powderTypeList: [					//检验状态的选项
					{
					  text: "双层粉"
					},
					{
					  text: "单层粉"
					}
				],
				showpowderType: false,
				powIndex: -1,
				oldcompletionQuantity: '',
				ptexamineNumber: 0
			}
		},
		methods: {
			// 点击按钮，启用扫码函数
			scanFun() {
				let that = this
				uni.scanCode({
					onlyFromCamera: true,
					success: function (res) {
						if(that.user.departmentName == '喷涂车间'){
							that.scanResult = res.result
						} else{
							that.scanResult = that.handleString(res.result)
						}
						that.getPlanNo()
					},
					fail() {
						console.log('识别失败')
					}
				})
			},
			handleString(str){
				let param = str.split('-')
				return param[0] + '-' + param[1]
			},
			// 弹出框“确认”绑定的函数
			// 在调用提交函数后关闭弹出框
			confirm() {
				this.allMeetStandard()
				this.show = false
			},
			// 弹出框“取消”绑定的函数
			// 关闭弹出框
			cancel() {
				this.show = false
			},
			// 打开弹出确认框
			showModal() {
				this.show = true
			},
			cancelVerifiedModal(){
				this.showQualified = false
			},
			unsameData(){
				if(this.oldcompletionQuantity != this.completionQuantity && this.user.departmentName == '喷涂车间'){
					this.List = []
				}
			},
			// 通过条形码获取planNo，随后与条形码内容一起发到后台获取列表信息
			async getPlanNo() {
				this.List = []
				// if(this.user.departmentName == '喷涂车间' && !this.machineId) return this.$refs.uTips.show({
				// 	title: '机台号不能为空！',
				// 	type: 'error',
				// 	duration: '2000'
				// })
				
				// if(this.user.departmentName == '喷涂车间' && !this.completionQuantity) return this.$refs.uTips.show({
				// 	title: '数量不能为空！',
				// 	type: 'error',
				// 	duration: '2000'
				// })
				let query = {
					deviceBarcode:this.scanResult
				}
				if(this.user.departmentName == '喷涂车间'){
					// query.machineId = this.machineId
					// query.completionQuantity = this.completionQuantity
					const qian4 = this.scanResult.slice(0, 4)  // 拿到前4位判断是不是5104
					if(qian4 !== '5104')  return this.$refs.uTips.show({
					title: '工单编码错误',
					type: 'warning',
					duration: '2000'
				})
				}
				console.log('query',query);
				const { data } = await getQCView(query)
				const errMsg = data.msg
				if(data.code == '1') {
					this.oldcompletionQuantity = this.completionQuantity
					this.$refs.uTips.show({
						title: '查询成功',
						type: 'success',
						duration: '2000'	
					})
					console.log('data',data);
					if(this.user.departmentName == '喷涂车间'){
						// data.data.isupSubmit = false
						this.List = data.data.map(item => {
							return {
								...item,
								powderType:'',
								mohou1:'',
								mohou2:'',
								mohou3:'',
								mohou4:'',
								mohou5:'',
							}
						})
						
						// this.completionQuantity = data.data.completionQuantity
					} else {
						this.List = data.data
						this.List.unshift({otherprocess: true,machineId:'',deviceBarcode:'',orderPlanNo:''})
					}
						console.log('list',this.List);
					
				} else {
					this.$refs.uTips.show({
						title: errMsg,
						type: 'warning',
						duration: '2000'
					})
				}
			},
			standard(machineId, deviceBarcode) {
				uni.navigateTo({
					url:"./setRightNum?machineId=" + machineId
				})
			},
			async getPtBindingUser(){
				// if(this.user.departmentName != '喷涂车间') {
					this.getPlanNo()
					// return
				// }
				if(uni.getStorageSync('ptexamineNumber')){ uni.removeStorageSync('ptexamineNumber') }
				// const { data } = await getPtBindingUser({ machineId: this.machineId })
				// console.log(this.scanResult);
				// const { data } = await getQCView({ deviceBarcode: this.scanResult })
				// console.log('data',data);
				// const errMsg = data.msg
				// if(data.code == '1') {
				// 	// if(data.data.department != '喷涂车间') return this.$refs.uTips.show({
				// 	// 	title: '该机台不属于喷涂车间！',
				// 	// 	type: 'warning',
				// 	// 	duration: '2000'
				// 	// })
				// 	this.getPlanNo()
				// } else {
				// 	this.$refs.uTips.show({
				// 		title: errMsg,
				// 		type: 'warning',
				// 		duration: '2000'
				// 	})
				// }
			},
			judgeStyle(value) {
				if(this.user.departmentName != '喷涂车间') return
				if(!this.List[value].isupSubmit) {
					this.onlineStyle[value] = "background-color: #FFEFD5;"
				} else {
					this.onlineStyle[value] = "background-color: #90EE90;"
				}
			},
			//将对应的数据传递到errorList页面
			goFillForm(item,index) {
				if(this.user.departmentName == '喷涂车间' && this.List[index].isupSubmit) return this.$refs.uTips.show({
					title: '已经提交参数，请勿重复提交！',
					type: 'warning',
					duration: '2000'
				})
				let items = Object.assign({},item)
				console.log('items',items);
				if(items.jobNumber==null){items.jobNumber = ''}
				if(items.jobNumber2==null){items.jobNumber2 = ''}
				if(items.jobNumber3==null){items.jobNumber3 = ''}
				let url = "./CNCqualityList?machineId=" + items.machineId
						+ '&deviceBarCode=' + items.deviceBarcode
						+ '&jobNumber=' + items.jobNumber
						+ '&jobNumber2=' + items.jobNumber2
						+ '&jobNumber3=' + items.jobNumber3
						+ '&orderPlanNo=' + items.planNo
						+ '&technologyNo=' + items.component
						+ '&scanCount=' + items.scanCount
						+ '&processName=' + items.processName
				if(this.user.departmentName == '喷涂车间') {
					url = url + '&sectionNo=' + items.msiSectionNo +
					 '&completionQuantity=' + this.completionQuantity + '&MaxexamineNumber=' + this.completionQuantity + '&ptindex=' + index
					if(uni.getStorageSync('ptexamineNumber')){ uni.removeStorageSync('ptexamineNumber') }
				}
				uni.navigateTo({
					url
				})
			},
			isQualified(index){
				if(this.user.departmentName == '喷涂车间'){
					// if(this.List.some(item => !item.isupSubmit)) return this.$refs.uTips.show({
					// 	title: '请先全部提交参数！',
					// 	type: 'warning',
					// 	duration: '2000'
					// })
					this.showQualified = true
				} else {
					this.show1 = true
				}
			},
			confirmQualified(){
				this.allMeetStandard()
			},
			async allMeetStandard(){
				let query = {
					orderType: this.scanResult.split('-')[0],
					workOrderNo: this.scanResult.split('-')[1],
					// MesCompletionQuantity: this.completionQuantity
					MesCompletionQuantity: parseInt(this.completionQuantity) - parseInt(this.ptexamineNumber)
				}
				let data = await commitPtDisqualificationInWarehouse(query)
				let errMsg = data.data.msg
				if(data.data.code == '1') {
					this.$refs.uTips.show({
						title: errMsg,
						type: 'success',
						duration: '2000'
					})
					this.List = []
					if(uni.getStorageSync('ptexamineNumber')){ uni.removeStorageSync('ptexamineNumber') }
				} else {
					this.$refs.uTips.show({
						title: errMsg,
						type: 'warning',
						duration: '2000'
					})
				}
			},
			powderTypeSelect(index){
				uni.hideKeyboard();
				this.List[this.powIndex].powderType = this.powderTypeList[index].text
				// this.powderType = this.powderTypeList[index].text
			},
			addRecord(item){
				if((parseInt(this.completionQuantity) - this.List.length)<1){
					return this.$refs.uTips.show({
						title: '超过可新增数量!',
						type: 'warning',
						duration: '2000'
					})
				} else {
					let obj = Object.assign({},item)
					obj.powderType = '',
					obj.mohou1 = '',
					obj.mohou2 = '',
					obj.mohou3 = '',
					obj.mohou4 = '',
					obj.mohou5 = '',
					obj.isupSubmit = false
					this.List.push(obj) 
				}
				
			},
			async upSubmit(item,index){
				console.log('提交参数',index,this.List[index])
				if(this.List[index].isupSubmit) return this.$refs.uTips.show({
					title: '已经提交参数，请勿重复提交！',
					type: 'warning',
					duration: '2000'
				})
				if(!item.powderType) return this.$refs.uTips.show({
					title: '粉末类型不能为空!',
					type: 'warning',
					duration: '2000'
				})
				if(!item.mohou1) return this.$refs.uTips.show({
					title: '膜厚1不能为空!',
					type: 'warning',
					duration: '2000'
				})
				if(!item.mohou2) return this.$refs.uTips.show({
					title: '膜厚2不能为空!',
					type: 'warning',
					duration: '2000'
				})
				if(!item.mohou3) return this.$refs.uTips.show({
					title: '膜厚3不能为空!',
					type: 'warning',
					duration: '2000'
				})
				if(!item.mohou4) return this.$refs.uTips.show({
					title: '膜厚4不能为空!',
					type: 'warning',
					duration: '2000'
				})
				if(!item.mohou5) return this.$refs.uTips.show({
					title: '膜厚5不能为空!',
					type: 'warning',
					duration: '2000'
				})
				let query = {
					deviceBarcode: this.scanResult,
					machineId: this.machineId,
					powderType: item.powderType,
					mohou1: parseFloat(item.mohou1),
					mohou2: parseFloat(item.mohou2),
					mohou3: parseFloat(item.mohou3),
					mohou4: parseFloat(item.mohou4),
					mohou5: parseFloat(item.mohou5),
					checkJobNumber: this.user.jobNumber,
					state: '1'
				}
				console.log('query',query);
				// query.mohouAvg = ((parseFloat(this.mohou1)+parseFloat(this.mohou2)+parseFloat(this.mohou3)+parseFloat(this.mohou4)+parseFloat(this.mohou5))/5).toFixed(2)
				let data = await saveMouHou(query)
				let errMsg = data.data.msg
				if(data.data.code == '1') {
					// this.List.splice(index,1)
					this.List[index].isupSubmit = true
					this.$refs.uTips.show({
						title: errMsg,
						type: 'success',
						duration: '2000'
					})
					this.judgeStyle(index)
				} else {
					this.$refs.uTips.show({
						title: errMsg,
						type: 'warning',
						duration: '2000'
					})
				}
			}
		},
		onShow() {
			console.log('refresh')
			if(uni.getStorageSync('ptexamineNumber')){
				// this.completionQuantity = this.completionQuantity - parseInt(uni.getStorageSync('ptexamineNumber').ptexamineNumber)
				// this.List.splice(0,parseInt(uni.getStorageSync('ptexamineNumber').ptexamineNumber))
				this.List[uni.getStorageSync('ptexamineNumber').ptindex].isupSubmit = true
				this.ptexamineNumber = this.ptexamineNumber + uni.getStorageSync('ptexamineNumber').ptexamineNumber
				this.judgeStyle(uni.getStorageSync('ptexamineNumber').ptindex)
			}
		},
		onLoad() {
			if(uni.getStorageSync('user').departmentName == '焊接车间'){
				uni.setNavigationBarTitle({
					title: '质量检查'
				});
			} else if(uni.getStorageSync('user').departmentName == '喷涂车间'){
				uni.setNavigationBarTitle({
					title: '喷涂质检'
				});
			}
		},
		onUnload() {
			uni.removeStorageSync('ptexamineNumber')
		}
	}
</script>

<style lang="scss" scoped>
.page-font {
	font-size: 40rpx;
}
.list-item-wrapper {
	margin: 20rpx;
	// padding: 20rpx;
	border-radius: 10rpx;
	background-color: #FFEFD5;
}
.list-item {
	display: flex;
	justify-content: space-between;
	font-size: 16px;
	margin: 10rpx 10rpx;
}
.buttonGap {
	margin-top: 20rpx;
}
// 上线下线按钮样式
.operateBtn {
	display: flex;
	flex-wrap: nowrap;
	justify-content: space-around;
}
.inputTips{
	font-size: 18rpx;
	color: #F56C6C;
}
.isupSubmit{
	background-color: #90EE90;
}
</style>
